'''
/*   Copyright 2012 Fernando Moreno Ruiz All Rights Reserved.
 * 
 * 	 This file is part of pyPhotosMixer.
 *
 *   pyPhotosMixer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   pyPhotosMixer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with pyPhotosMixer.  If not, see <http://www.gnu.org/licenses/>.
 * */
'''
'''
Created on 24/02/2012

@author: Fernando
'''
import os
import shutil
import EXIF
from PIL import Image
from PySide import QtCore
class EXIFQt(QtCore.QObject):
    '''
    classdocs
    '''
    @QtCore.Slot(str, result=str)
    def getDatePicture(self, fileName):
        try:
            fileName = fileName.replace("file:///", "")
            f = open(fileName, 'rb')
            tags = EXIF.process_file(f)
        except:
             return "error"
#        if  tags.values()==None: 
#           
#        else:
#        print tags['Image DateTime']
        return str(tags['Image DateTime'])
#        return "prueba"

    @QtCore.Slot()
    def resetThumbnailDir(self):
        pathThumbnail = os.getcwd() + "\\thumbnails"
        try:
            print "resetThumbnail Dir"
            shutil.rmtree(pathThumbnail, True)
        except:
            print "error borrando:" + pathThumbnail
            return "error"
        
        
    @QtCore.Slot(str, result=str)
    def createThumbail(self, fileName):
        try:
            pathThumbails = os.getcwd() + "/thumbnails/"
            if not os.path.exists(pathThumbails):
               os.mkdir(pathThumbails)
            fileName = fileName.replace("file:///", "")
            img = Image.open(fileName)
            # convert to thumbnail image
            img.thumbnail((300, 300), Image.ANTIALIAS)
            # prefix thumbnail file with T_
            pathThumbailFile = pathThumbails + os.path.basename(fileName) + "_t"
            i = 0;
            while(os.path.exists(pathThumbailFile)):
                pathThumbailFile = pathThumbailFile + i
                + +i
            img.save(pathThumbails + os.path.basename(fileName) + "_t", "JPEG")
            print "P:" + fileName
            return "file:///" + pathThumbails.replace("\\", "/") + os.path.basename(fileName) + "_t"
        except:
             return "error"
   
    def __init__(self, selfparams):
        '''
        Constructor
        '''
        QtCore.QObject.__init__(self)
        
